/**
 * 首诊主诉转换器
 * @param {*} data
 */
export function chiefComplaint(data) {
  const {
    attr1,
    attr2,
    attr3,
    attr4,
    attr5,
    attr6,
    attr7,
    isRecentTakedMedicines,
    recentTakedMedicineList,
    hasAdjustMedicalRequest,
    adjustRequestContent,
    attr8,
  } = data

  let str = ''
  str += attr1Convert(attr1)
  if (str) {
    str += attr2to7(attr2, attr3, attr4, attr5, attr6, attr7)

    str += takedMedicine(
      isRecentTakedMedicines,
      recentTakedMedicineList,
      hasAdjustMedicalRequest,
      adjustRequestContent
    )
    if (attr8) str += `。其他：${attr8}`
    return str
  } else {
    return ''
  }
}

function attr1Convert(str = '') {
  if (splitToLength(str, 2)) {
    return '反复咳嗽伴喘息发作'
  } else {
    const cough = str.includes('咳嗽') ? '反复咳嗽发作' : ''
    const wheezing = str.includes('伴喘息') ? '反复喘息发作' : ''
    return cough || wheezing
  }
}
function attr2to7(attr2, attr3, attr4, attr5, attr6, attr7) {
  let str = ''
  if (attr2 || attr3 || attr4) {
    if (attr2) str += `${attr2}年`
    if (attr3) str += `${attr3}月`
    if (attr4) str += `${attr4}日`
  }
  if (attr5 || attr6 || attr7) {
    str += '，再发'
    if (attr5) str += `${attr5}年`
    if (attr6) str += `${attr6}月`
    if (attr7) str += `${attr7}日`
  } else {
    str += '。'
  }
  return str
}
/**
 * 是否规律用药，是否调整过用药情况
 * @param {*} isRecentTakedMedicines
 * @param {*} takedMedicineList
 * @param {*} hasAdjust
 * @param {*} adjustContent
 */
function takedMedicine(
  isRecentTakedMedicines,
  takedMedicineList,
  hasAdjust,
  adjustContent
) {
  let str = ''
  if (isRecentTakedMedicines) {
    str +=
      isRecentTakedMedicines == '有' ? '有规律使用药物' : '没有规律使用药物'
    try {
      takedMedicineList = JSON.parse(takedMedicineList)
    } catch (e) {
      takedMedicineList = []
    }
    if (takedMedicineList.length) {
      let medicineListStr = takedMedicineList.map((item) => {
        let frequencyStr =
          item.commonName + (item.frequency ? '，' + item.frequency : '')
        let regularUsedStr = item.duration
          ? '，使用' + item.duration + '天'
          : ''
        return `${frequencyStr}${regularUsedStr}`
      })
      medicineListStr = medicineListStr.join('、')
      str += '（使用：' + medicineListStr + ')'
    }
  }

  if (hasAdjust) {
    str +=
      `，${hasAdjust}调整过用药方案` +
      (adjustContent ? `，${adjustContent}` : '')
  }
  return str
}
/**
 * 按逗号分隔后，应该有多少长度，符合true, 不符合和其他异常返回false
 * @param {*} str 字符串
 * @param {*} length 长度
 */
function splitToLength(str, length) {
  const tmp = str.split(',')
  if (tmp && tmp.length === length) {
    return true
  } else {
    return false
  }
}
